package me.lubinn.Vicincantatio;

import java.io.*;
import java.util.*;


import org.bukkit.GameMode;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityListener;

import me.lubinn.Vicincantatio.Spells.DurationSpell;
import me.lubinn.Vicincantatio.Spells.Tueri;

// Referenced classes of package me.orion.Vicincantatio:
//            Vicincantatio

public class VicincantatioEntityListener extends EntityListener
{

    public static Vicincantatio plugin;
	private boolean flag = false;
	private boolean noTimer = true;
	protected Timer timer = new Timer();

    public VicincantatioEntityListener(Vicincantatio instance)
    {
        plugin = instance;
    }

    public void onEntityDamage(EntityDamageEvent event)
    {
    	
        if(event.getEntity() instanceof Player)
        {
        	//This shit is just getting the name clumsily
        	
//            String playername = "none";
//            List<Player> players = event.getEntity().getWorld().getPlayers();
//            for(Iterator<Player> iterator = players.iterator(); iterator.hasNext();)
//            {
//                Player player = iterator.next();
//                if(player.getEntityId() == event.getEntity().getEntityId())
//                {
//                    playername = player.getName();
//                }
//            }
	
        	
        	//Tada
        	String playername = ((Player) event.getEntity()).getName();
            
            try
            {
                FileInputStream in = new FileInputStream(Vicincantatio.VicincantatioLog);
                Vicincantatio.prop.load(in);
                String LastSpell = Vicincantatio.prop.getProperty(playername);
                in.close();
               
            	//
                if(LastSpell.trim().equalsIgnoreCase("tueri") && !flag)
                {

                	if(noTimer)
                	{
                		timer.scheduleAtFixedRate(task, 0, 3000);
                		noTimer = false;
                	}
                	//Tueri here
                	Player eventPlayer = (Player)event.getEntity();
                	//eventPlayer.setGameMode(GameMode.CREATIVE);
                    
                	//eventPlayer.setHealth(20);
                    //eventPlayer.setRemainingAir(eventPlayer.getMaximumAir());
                    //eventPlayer.setFireTicks(0);
                    //eventPlayer.setNoDamageTicks(1);
                    //event.setCancelled(true);
                    
                	
                	//This is inside if !flag, hence doesnt get called evah
                	
//                    //After all this is done, if it were to be true, reset for future
//                    if(flag)
//                    {
//                    	flag = false;
//                    	noTimer = true;
//                    	//eventPlayer.setGameMode(GameMode.SURVIVAL);
//                    }
                	
                    //Tueri.protect(event,3);
                    try
                    {
                        FileOutputStream out = new FileOutputStream(Vicincantatio.VicincantatioLog);
                        Vicincantatio.prop.put(playername, "damage");
                        out.flush();
                        out.close();
                    }
                    catch(IOException ex)
                    {
                        ex.printStackTrace();
                    }
                } 
                else
                if(LastSpell.trim().split(" ")[0].equalsIgnoreCase("celeritas") && event.getCause() == org.bukkit.event.entity.EntityDamageEvent.DamageCause.FALL)
                {
                    event.setCancelled(true);
                }
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
    
    
    TimerTask task = new TimerTask()
	{

		public void run()
		{    			
			int iterations = 0;
			if (iterations >= 1)
			{
				flag = true;
				this.cancel();
			}
			iterations++;
		}

	};
	

}
